﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

using EZCRS.HCMS.Bll;
using EZCRS.HCMS.Bll.baseinfo;

using EZCRS.HCMS.Entity;
using EZCRS.HCMS.Bll.AIR;
using EZCRS.HCMS.Web;

namespace EZCRS.HCMS.Web.manage.AIR
{
    public partial class calendar_report : ManageBase
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (base.BasHotelId == 0)
                Response.Write(CFun.AlertMeg(Resources.Site.MustHotel, "back", ""));

            Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
            Response.Cache.SetNoStore();

            if (Request.QueryString["action"] != null)
            {
                if (Request.QueryString["args"] != null)
                {
                    if (Request.QueryString["type"] != null)
                    {
                        if (Request.QueryString["type"] == "1")
                        {
                            Response.Write(GetRateJSON(Request.QueryString["args"]));
                            Response.End();
                        }
                        else
                        {
                            Response.Write(GetAI_JSON(Request.QueryString["args"]));
                            Response.End();
                        }
                    }
                }

                return;
            }

            if (!IsPostBack)
            {
                CheckModule(68);

                BindData();
                btnNext.Enabled = false;
                btnPrv.Enabled = false;
            }
        }

        /// <summary>
        /// 返回价格的JSON
        /// </summary>
        /// <returns></returns>
        private string GetRateJSON(string Args)
        {

            string[] strValue = Args.Split(',');

            if (strValue.Length >= 5)
            {
                //0 year
                //1 level
                //2 room
                //3 plan
                //4 date

                string[] strSplit = strValue[3].Split('-');
                string strWhere = "property_id =" + base.BasPropertyId + " AND level = '" + strValue[1] +
                    "' AND RoomCode = '" + strSplit[0] + "' AND PlanCode = '" + strSplit[1] + "' AND Year = " + strValue[0];

                BllHCRS_AIR bll = new BllHCRS_AIR();

                List<HCRS_AIR> airData = bll.getall(strWhere);

                if (airData.Count > 0)
                {
                    HotelRateProvider hrp = new HotelRateProvider();

                    HCRS_AIR air = airData[0];

                    hrp.Load(air);

                    DateTime date = Convert.ToDateTime(strValue[0] + "-" + strValue[4]);

                    DataRow dr = hrp.GetRow(date);

                    if (dr != null)
                    {
                        bool bBF = false;

                        bool.TryParse(dr["BF"].ToString(), out bBF);

                        string strBF = "包含";

                        if (bBF)
                            strBF = "包含";
                        else
                            strBF = "不含";

                        string strJSON =

                            "{rate1: " + dr["Rate1"].ToString() +
                            ", rate2: " + dr["Rate2"].ToString() +

                            ", rate3:" + dr["Rate3"].ToString() +
                            ", rate4:" + dr["Rate4"].ToString() +
                            ", bed:" + dr["Bed"].ToString() +

                            ", hasBF:\"" + strBF + "\"" +
                            ", BFnum:" + dr["BFNum"].ToString() +
                            ", BFrate:" + dr["BFrate"].ToString() +
                            ", Date:\"" + date.ToString("yyyy-MM-dd") + "\"" +
                            "}";

                        return strJSON;
                    }
                }
            }

            return "{rate1:0 , rate2:0 , rate3:0, rate4:0, hasBF:, BFNum:0, BFRate:0, Date:\"\" }";
        }

        /// <summary>
        /// 返回AI信息JSON
        /// </summary>
        /// <param name="Args"></param>
        /// <returns></returns>
        private string GetAI_JSON(string Args)
        {

            string[] strValue = Args.Split(',');

            if (strValue.Length >= 5)
            {
                //0 year
                //1 level
                //2 room
                //3 plan
                //4 date

                string[] strSplit = strValue[3].Split('-');

                string strWhere = "";

                if (strValue[1] == "L")
                {
                    //plan
                    strWhere = "property_id =" + base.BasPropertyId + " AND level = '" + strValue[1] +
                        "' AND RoomCode = '" + strSplit[0] + "' AND PlanCode = '" + strSplit[1] + "' AND Year = " + strValue[0];
                }
                else if (strValue[1] == "R")
                {
                    //room
                    strWhere = "property_id =" + base.BasPropertyId + " AND level = '" + strValue[1] +
                        "' AND RoomCode = '" + strValue[2] + "' AND Year = " + strValue[0];
                }
                else
                {
                    //property

                    strWhere = "property_id =" + base.BasPropertyId + " AND level = '" + strValue[1] + "' AND Year = " + strValue[0];
                }

                BllHCRS_AIR bll = new BllHCRS_AIR();

                List<HCRS_AIR> airData = bll.getall(strWhere);

                if (airData.Count > 0)
                {
                    HotelAIProvider haip = new HotelAIProvider();

                    HCRS_AIR air = airData[0];

                    haip.Load(air);

                    DateTime date = Convert.ToDateTime(strValue[0] + "-" + strValue[4]);

                    DataRow dr = haip.GetRow(date);

                    if (dr != null)
                    {

                        int iCurInv = 0;

                        int.TryParse(dr["CurInv"].ToString(), out iCurInv);
                        

                        string strJSON =
                            "{" +
                            "inventory: " + dr["Inventory"].ToString() +

                            ", state: \"" + dr["state"].ToString() + "\"" +
                            ", curinv: " + iCurInv.ToString() +

                            ", min:" + dr["min"].ToString() +
                            ", max:" + dr["max"].ToString() +
                            ", adv:" + dr["adv"].ToString() +

                            ", gua:\"" + dr["gua"].ToString() + "\"" +
                            ", cxl:\"" + dr["cxl"].ToString() + "\"" +
                            ", Date:\"" + date.ToString("yyyy-MM-dd") + "\"" +
                            "}";

                        return strJSON;
                    }
                }
            }

            return "{ inventory:0 , curinv:0, state:\"\" , min:0, max:0, adv: 0, gua:\"\", cxl:\"\", Date:\"\" }";
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

            int iYear = DateTime.Now.Year;

            hidValue.Value = iYear.ToString() + "," + ddlLevel.SelectedValue + "," + ddlRoomType.SelectedValue + "," + ddlPlan.SelectedValue;

            ShowReport(iYear, ddlLevel.SelectedValue, ddlRoomType.SelectedValue, ddlPlan.SelectedValue);
        }

        private void BindData()
        {
            int iLan = 0;

            if (base.CurrentLanguage != "zh-CN")
                iLan = 1;

            Bllh_roomcode bl = new Bllh_roomcode();

            List<HCRS_RoomCode> lst = bl.getitem("pid = " + base.BasPropertyId + " AND avaliable = 1");

            if (lst != null)
            {
                foreach (HCRS_RoomCode room in lst)
                    ddlRoomType.Items.Add(new ListItem(room.roomname, room.roomcode));
            }

            //ddlRoomType.Items.Insert(0, new ListItem("--房型--", ""));

            BllRatePlan bllPlan = new BllRatePlan();

            int iRowCount = 0;
            
            List<HCRS_Plan> lstPlan =
                bllPlan.getListByPage(base.BasPropertyId, 1, iLan, string.Empty, 1, 999, out iRowCount);

            if (lstPlan != null)
            {
                foreach (HCRS_Plan plan in lstPlan)
                    ddlPlan.Items.Add(new ListItem(plan.PlanCode, plan.PlanCode));
            }

        }

        /// <summary>
        /// 显示数据
        /// </summary>
        /// <param name="Year">年份</param>
        /// <param name="Level">计划级别</param>
        private void ShowReport(int Year, string Level, string RoomCode, string PlanCode)
        {

            lblYear.Text = Year.ToString();

            btnNext.Enabled = true;
            btnPrv.Enabled = true;

            BllHCRS_AIR bll = new BllHCRS_AIR();

            string strWhere = "";

            if (Level == "L")
            {
                //计划级
                string[] strSplit = PlanCode.Split('-');

                strWhere = "property_id =" + base.BasPropertyId + " AND level = '" + Level +
                    "' AND RoomCode = '" + strSplit[0] + "' AND PlanCode = '" + strSplit[1] + "' AND Year = " + Year.ToString();

            }
            else if (Level == "R")
            {
                //房型级
                strWhere = "property_id =" + base.BasPropertyId + " AND level = '" + Level +
                    "' AND RoomCode = '" + RoomCode + "' AND Year = " + Year.ToString();
            }
            else
            {
                //酒店级
                strWhere = "property_id =" + base.BasPropertyId + " AND level = '" + Level +
                           "' AND Year = " + Year.ToString();
            }

            List<HCRS_AIR> airData = bll.getall(strWhere);

            if (airData.Count > 0)
            {

                //进行数据转换
                HotelAIProvider hap = new HotelAIProvider();

                hap.Load(airData[0]); //只取第一条记录

                DateTime dtDay = Convert.ToDateTime(Year.ToString() + "-01-01");

                lblYear.Text = Year.ToString();

                StringBuilder sb = new StringBuilder();

                sb.Append("<div class='calendar_warp'>");

                for (int i = 1; i < 13; i++)
                {
                    sb.Append("<div class='month' id=\"month_" + i.ToString() + "\">");

                    sb.Append("<h3>" + i.ToString() + "月</h3>");
                    sb.Append("<div class=\"days_title\">");
                    sb.Append("<span>周一</span><span>周二</span><span>周三</span><span>周四</span><span>周五</span><span>周六</span><span>周日</span>");
                    sb.Append("</div>"); //end of days_title div

                    sb.Append("<div class=\"days\">");

                    //每月的天数
                    int iSkip = 0;

                    int[] iMonthInventory = hap.GetInventoryList(i, true);    //实际房量

                    int[] iSettingInventory = hap.GetInventoryList(i, false); //设置的房量

                    string[] strMonthStatus = null;

                    if (Level == "P")
                        strMonthStatus = hap.GetStatusList(i);

                    while (dtDay.Month == i)
                    {

                        if (dtDay.Day == 1)
                        {
                            if (dtDay.DayOfWeek == DayOfWeek.Tuesday)
                            {
                                iSkip = 1;
                            }
                            else if (dtDay.DayOfWeek == DayOfWeek.Wednesday)
                            {
                                iSkip = 2;
                            }
                            else if (dtDay.DayOfWeek == DayOfWeek.Thursday)
                            {
                                iSkip = 3;
                            }
                            else if (dtDay.DayOfWeek == DayOfWeek.Friday)
                            {
                                iSkip = 4;
                            }
                            else if (dtDay.DayOfWeek == DayOfWeek.Saturday)
                            {
                                iSkip = 5;
                            }
                            else if (dtDay.DayOfWeek == DayOfWeek.Sunday)
                            {
                                iSkip = 6;
                            }

                            for (int j = 0; j < iSkip; j++)
                                sb.Append("<span></span>");

                        }


                        if (iSettingInventory[dtDay.Day - 1] == 0)
                        {
                            if (Level == "P" && !string.IsNullOrEmpty(strMonthStatus[dtDay.Day - 1]))

                                sb.Append("<span><a href='#' class='a_ai' rel='" +
                                    dtDay.Month.ToString() + "-" +
                                    dtDay.Day.ToString() + "'>" +
                                    dtDay.Day.ToString() + "</a></span>");
                            else
                            {
                                sb.Append("<span>" + dtDay.Day.ToString() + "</span>");
                            }

                        }
                        else
                        {
                            if (Level == "L")
                            {
                                //L级别可显示价格
                                sb.Append("<span>" +
                                    "<a href='#' rel='" +
                                    dtDay.Month.ToString() + "-" +
                                    dtDay.Day.ToString() + "' class='a_rate'>" +
                                    dtDay.Day.ToString() + "</a>" +
                                    "<br />" +
                                    "<a href='#' rel='" +
                                    dtDay.Month.ToString() + "-" +
                                    dtDay.Day.ToString() + "' class='a_ai'>" +
                                    "房:" + iMonthInventory[dtDay.Day - 1].ToString() + "</a>" +
                                    "</span>");
                            }
                            else
                            {
                                sb.Append("<span>" +
                                    dtDay.Day.ToString() +
                                    "<br />" +
                                    "<a href='#' rel='" +
                                    dtDay.Month.ToString() + "-" +
                                    dtDay.Day.ToString() + "' class='a_ai'>" +
                                    "房:" + iMonthInventory[dtDay.Day - 1].ToString() + "</a>" +
                                    "</span>");
                            }

                        }


                        dtDay = dtDay.AddDays(1);
                    }



                    sb.Append("</div>");    //end of days div

                    sb.Append("</div>");    //end of month div
                }

                sb.Append("</div>");

                litHtml.Text = sb.ToString();
            }
            else
            {
                litHtml.Text = "No data...";

            }
        }

        protected void btnPrv_Click(object sender, EventArgs e)
        {
            int iYear = 0;

            int.TryParse(lblYear.Text, out iYear);

            if (iYear > 2000)
                iYear--;

            hidValue.Value = iYear.ToString() + "," + ddlLevel.SelectedValue + "," + ddlRoomType.SelectedValue + "," + ddlPlan.SelectedValue;
            
            ShowReport(iYear, ddlLevel.SelectedValue, ddlRoomType.SelectedValue, ddlPlan.SelectedValue);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            int iYear = 0;

            int.TryParse(lblYear.Text, out iYear);

            if (iYear < 3000)
                iYear++;

            hidValue.Value = iYear.ToString() + "," + ddlLevel.SelectedValue + "," + ddlRoomType.SelectedValue + "," + ddlPlan.SelectedValue;

            ShowReport(iYear, ddlLevel.SelectedValue, ddlRoomType.SelectedValue, ddlPlan.SelectedValue);
        }
    }
}
